package com.bzyd.mp.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bzyd.mp.datascope.DataScopePermissionHandler;
import com.bzyd.mp.datascope.strategy.CustomDataScopeStrategy;
import com.bzyd.mp.datascope.strategy.OwnChildDeptDataScopeStrategy;
import com.bzyd.mp.datascope.strategy.SelfDataScopeStrategy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Mybatis配置类
 * Created by Xianyue Dang on 2021/8/24
 */
@Configuration
@MapperScan("com.bzyd.mp.mapper") //mapper扫描路径
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

//        //数据权权限插件
//        DataPermissionInterceptor dataPermissionInterceptor = new DataPermissionInterceptor();
//        // 添加自定义的数据权限处理器
//        dataPermissionInterceptor.setDataPermissionHandler(dataScopePermissionHandler());
//        interceptor.addInnerInterceptor(dataPermissionInterceptor);

        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 自定义数据权限处理器
     */
    @Bean
    public DataScopePermissionHandler dataScopePermissionHandler() {
        return new DataScopePermissionHandler();
    }

    /**
     * 只能看自己的数据权限策略
     */
    @Bean
    public SelfDataScopeStrategy selfDataScopeStrategy() {
        return new SelfDataScopeStrategy();
    }

    /**
     * 可以看自己所在部门及子部门的数据策略
     */
    @Bean
    public OwnChildDeptDataScopeStrategy ownChildDeptDataScopeStrategy() {
        return new OwnChildDeptDataScopeStrategy();
    }

    /**
     * 自定义策略：仅部门老大角色可以查看自己部门及子部门的 员工只能查询自己的
     */
    @Bean
    public CustomDataScopeStrategy customDataScopeStrategy() {
        return new CustomDataScopeStrategy();
    }
}
